package com.hospital.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.wx.common.CommonResult;
import com.hospital.wx.controller.form.AddReplyForm;
import com.hospital.wx.service.RepliesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/patient/user/reply")
@Tag(description = "回复评论管理接口", name = "RepliesController")
@Slf4j
public class RepliesController {
    @Autowired
    private RepliesService repliesService;

    @PostMapping("/add")
    @Operation(summary = "添加回复接口")
    @CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
    public CommonResult addReply(@Valid @RequestBody AddReplyForm form) {
        try {
            Map<String, Object> param = BeanUtil.beanToMap(form);
            int rows = repliesService.addReply(param);
            if (rows == 1) {
                return CommonResult.ok().put("rows", rows);
            }
            return CommonResult.failed("添加失败");
        } catch (Exception e) {
            log.error("添加过程中出现错误", e);
            return CommonResult.failed("添加过程中出现错误");
        }
    }

    @PostMapping("/delete")
    @Operation(summary = "删除回复")
    public CommonResult deleteReply(@RequestParam("replyId") Integer replyId) {
        try {
            int rows = repliesService.deleteReply(replyId);
            if (rows==1) {
                return CommonResult.ok().put("rows", rows);
            }
            return CommonResult.failed("删除回复失败");
        } catch (Exception e) {
            log.error("删除过程中出现异常", e);
            return CommonResult.failed("删除回复过程中出现异常");
        }

    }
}
